USE [BusSystem]
GO

/****** Object:  UserDefinedFunction [dbo].[GetLastOdoMeterBus]    Script Date: 06/08/2010 00:12:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GetLastOdoMeterBus]
(
	-- Add the parameters for the function here
	@BusID VARCHAR(20),
	@OilDate SMALLDATETIME
)
RETURNS DECIMAL
AS
BEGIN
	-- Declare the return variable here
	DECLARE @OdometerBus DECIMAL

	-- Add the T-SQL statements to compute the return value here
    SELECT TOP 1 @OdometerBus=OdometerBus
	FROM [Bus_BusOilManage]
	WHERE BusID=@BusID AND OilDate<@OilDate AND OdometerBus>0
	ORDER BY OilDate DESC,DocID DESC

	-- Return the result of the function
	RETURN @OdometerBus

END

GO


